<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>leetcode1-3</title>
</head>

<body>
    <script>
        // 1. 输入 数组[2,7,11,15] 数字：目标 9
        // 检测数组中，两个数字相加，正好等于目标，然后返回这两个数字的下标值
        // 函数返回 [0,1]
        var arr1 = [2, 7, 11, 15];
        var twoAdd = function(arr, target) {
            for (var i = 0; i < arr.length; i++) {
                for (var j = i + 1; j < arr.length; j++) {
                    if (arr[i] + arr[j] === target) {
                        return [i, j];
                    }
                }
            }
            return false;
        }
        console.log(twoAdd(arr1, 9));

        //2. 输入两个长度相等数组
        // （2 4 3）  （5 6 4）
        // 输出 7 0 8
        // 2+5=7
        // 4+6=10  --0
        // 3+4+1=8
        var arr2 = [2, 4, 3];
        var arr3 = [5, 6, 4];
        // var arrAdd = function(x, y) {
        //     var as = [];
        //     var a = 0;
        //     if (x.length != y.length) {
        //         console.log("两个数组不一样");
        //     } else {
        //         for (var i = 0; i < x.length; i++) {

        //             a = a + x[i] + y[i];
        //             as.push(a % 10);
        //         }
        //         console.log(as);
        //         return as;
        //     }
        // }
        // arrAdd(arr2, arr3);

        var arrsum = function(arr1, arr2) {
            if (arr1.length !== arr2.length) {
                return false;
            }
            var ret = new Array(arr1.length);
            //答案数组初始化为0；
            for (var i = 0; i < ret.length; i++) {
                ret[i] = 0;
            }
            for (var i = 0; i < arr1.length; i++) {
                ret[i] = ret[i] + arr1[i] + arr2[i];
                if (ret[i] >= 10) {
                    if (i < arr1.length - 1) { //最后一个，省略掉（不进位）
                        ret[i + 1] = 1; //防止下标溢出
                    }
                    ret[i] = ret[i] % 10;
                }
            }
            return ret;
        }

        console.log(arrsum(arr2, arr3));




        // 3. 找出字符串中最长不重复字符串的长度
        // abcabcbb  abc  --3
        // bbbbbbb   b  --1
        // pwwkew    wke  --3  (pw wke kew)
        var strings = "pwwkewa";
        var strNum = function(str) {
            var now = "";
            var as = [];
            var sizes = 0;
            for (var i = 0; i < str.length; i++) {
                now = str.slice(i, i + 1);
                var index = as.indexOf(now);
                if (index == -1) {
                    as.push(now);
                } else {
                    if (as.length > sizes) {
                        sizes = as.length;
                    }
                    as = as.slice(index + 1, as.length);
                    as.push(now);
                }
            }
            if (as.length > sizes) {
                sizes = as.length;
            }
            return sizes;
        }
        console.log(strNum(strings));
    </script>
</body>

</html>